package com.luyuan.crm.sale.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.MemberCoupon;
import com.luyuan.crm.member.model.MemberCouponType;
import com.ruijie.util.StringUtils;

@Component("couponDeductionConverter")
public class CouponDeductionConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        }
        String[] s = value.split(":");
        MemberCoupon coupon = new MemberCoupon();
        coupon.setId(value);
        coupon.setCouponType(MemberCouponType.valueOf(s[0]));
        coupon.setAmount(Integer.valueOf(s[1]));
        return coupon;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }

        MemberCoupon coupon = (MemberCoupon) value;
        return coupon.getCouponType() + ":" + coupon.getAmount();
    }
}
